<?php

namespace App\Http\Controllers\Chat;

use App\Http\Controllers\Auth\JwtToken;
use App\Models\Chat\ChatEmployDepartment;
use App\Models\Chat\ChatEmployeeMessage;
use App\Models\Chat\ChatGroup;
use App\Models\Chat\ChatGroupMessage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator;
/**
 * @group 聊天历史记录获取接口
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class ChatMessagesController extends Controller
{

    /**
     * 查看聊天信息 employee 筛选某个用户的聊天信息
     *
     * @bodyParam receiver_id int optional 筛选接受者的id
     * @bodyParam chat_id int optional 筛选某个频道的聊天信息
     * @bodyParam sender_id int optional 筛选发送者的id
     *
     *
     * @queryParam sender->id 消息发送者的主键id号
     * @queryParam sender->name 消息发送者的名称
     * @queryParam message->发送的消息
     * @queryParam file 发送的文件 和其他文件接一样
     * @queryParam created_at 消息发送的时间
     * @queryParam personInfo 员工的信息和员工信息接口一样
     */
    public function index(Request $request){
        $employee = $request->user_id;
        $chatId = $request->input('chat_id',null);
        $sender = $request->input('sender_id',null);
        if($chatId){
            $message = ChatGroupMessage::with(['sender','file','chat'])->where('chat_id',$chatId)->get()->toArray();
            $users = ChatGroup::with(['users','departments'])->find($chatId)->toArray();
            return response()->json(['messages'=>$message,'users'=>$users]);
        }elseif($sender){
            $query = ChatEmployeeMessage::with(['file','sender'])->where(['sender_id'=>$sender,'receiver_id'=>$employee])->get();
            return response()->json($query);
        }
        return response()->json(ChatGroupMessage::all());
    }
}
